home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
58606
/
58606.xpi
/
chrome
/
translator.jar
/
content
/
translator.js
< prev
next >
Wrap
Text File
|
2010-02-07
|
8KB
|
300 lines
(function(namespace, $)
{
namespace.Translator = function(prefsManager, ui)
{
this.prefs = prefsManager;
this.ui = ui;
};
namespace.Translator.STATUS_TRANSLATED = 1;
namespace.Translator.STATUS_NOT_DETECTED = 2;
namespace.Translator.STATUS_NOT_TRANSLATED = 3;
namespace.Translator.STATE_DISABLED = 0;
namespace.Translator.STATE_ENABLED = 1;
namespace.Translator.STATE_ERROR = 2;
namespace.Translator.prototype = {
initialized: false,
state: null,
ui: null,
prefs: null,
cache: {},
init: function()
{
// set event listeners
$(document).bind('translatorPreferencesChanged.translator', this.preferencesChangedHandler.bind(this));
$(document).bind('translatorStateSwitched.translator', this.stateSwitchedHandler.bind(this));
$(document).bind('translatorLanguageChanged.translator', this.languageChangedHandler.bind(this));
$(document).bind('translatorShowContextMenuSwitched.translator', this.showContextMenuSwitchedHandler.bind(this));
$(document).bind('translatorShowToolbarSwitched.translator', this.showToolbarSwitchedHandler.bind(this));
$(document).bind('translatorShowStatusBarSwitched.translator', this.showStatusBarSwitchedHandler.bind(this));
$(document).bind('translatorTranslateSelection.translator', this.translateSelectionHandler.bind(this));
this.initialized = true;
// initialize ui
this.ui.init();
// initial reload of translator
this.reload();
},
reload: function()
{
// enable/disable translator
if(this.prefs.getPref('enabled')) {
this.state = namespace.Translator.STATE_ENABLED;
this.ui.showDefaultState();
$(document).trigger('translatorEnabled.translator');
}
else {
this.state = namespace.Translator.STATE_DISABLED;
this.ui.showDisabledState();
$(document).trigger('translatorDisabled.translator');
}
// setup interface
this.setupUI();
// translate by selection
if(this.prefs.getPref('translate.selection') && this.state == namespace.Translator.STATE_ENABLED) {
$(document).trigger('translatorTranslateSelectionEnabled.translator');
}
else {
$(document).trigger('translatorTranslateSelectionDisabled.translator');
}
// translate by floating pane
if(this.prefs.getPref('translate.floating') && this.state == namespace.Translator.STATE_ENABLED) {
$(document).trigger('translatorTranslateFloating.translator', true);
}
else {
$(document).trigger('translatorTranslateFloating.translator', false);
}
},
setupUI: function()
{
// load list of selected languages
this.ui.loadLanguages(this.getSelectedLanguages());
// set language
var languageCode = this.prefs.getPref('language');
this.ui.setLanguage(languageCode, this.getLanguageName(languageCode));
// set appearances
this.ui.showContextMenu(this.prefs.getPref('context.menu'));
this.ui.showToolbar(this.prefs.getPref('toolbar'));
this.ui.showStatusBar(this.prefs.getPref('statusbar'));
this.ui.showStatusBarIcon(this.prefs.getPref('status.icon'));
this.ui.showStatusBarLabel(this.prefs.getPref('status.label'));
// set hotkey
this.ui.setHotKey(this.prefs.getPref('translate.hotkey.modifiers'), this.prefs.getPref('translate.hotkey.key'));
// enable/disable translation by hotkey
if(this.prefs.getPref('translate.hotkey') && this.state == namespace.Translator.STATE_ENABLED) {
this.ui.enableHotKey();
}
else {
this.ui.disableHotKey();
}
},
translate: function(text, callback)
{
if(!this.initialized || !this.isEnabled()) return;
if(text.length == 0) return;
if(text == this.cache.original) {
callback(namespace.Translator.STATUS_TRANSLATED, this.cache.translation, this.cache.notice);
return;
}
// run detection and translation
this.detect(text, function(response) {
if(!response || !response.language) {
callback(namespace.Translator.STATUS_NOT_DETECTED, text);
return;
}
this.translateRequest(text, response.language, this.prefs.getPref('language'), function(response, textWasLimited) {
if(!response || !response.translatedText) {
callback(namespace.Translator.STATUS_NOT_TRANSLATED, text);
return;
}
var noticeText = '';
var translatedText = response.translatedText;
// add limited message (if needed)
if(textWasLimited) {
translatedText += '...';
noticeText = $('#translator-properties').get(0).getString('truncatedTranslation');
}
// cache translation
this.cache.original = text;
this.cache.translation = translatedText;
this.cache.notice = noticeText;
// callback
callback(namespace.Translator.STATUS_TRANSLATED, translatedText, noticeText);
}.bind(this));
}.bind(this));
},
detect: function(text, callback)
{
if(!this.initialized || !this.isEnabled()) return;
if(text.length == 0) return;
this.detectRequest(text, callback);
},
detectRequest: function(text, callback)
{
if(text.length == 0) return;
// request params
var params = {
v: '1.0',
q: text.substr(0, 500) // limit text to 500 characters since it's still enough to detect language
};
// make request
$.ajax({
url: 'http://ajax.googleapis.com/ajax/services/language/detect',
data: params,
dataType: 'json',
success: function(data) {
callback(data.responseData);
}
});
},
translateRequest: function(text, sourceLanguage, destLanguage, callback)
{
if(text.length == 0) return;
var textWasLimited = false;
// 5000 characters is the limit set by google
if(text.length > 5000) {
text = text.substr(0, 5000);
textWasLimited = true;
}
// request params
var params = {
v: '1.0',
q: text,
langpair: sourceLanguage + '|' + destLanguage
};
// make request
$.ajax({
url: 'http://ajax.googleapis.com/ajax/services/language/translate',
type: 'POST',
data: params,
dataType: 'json',
success: function(data) {
callback(data.responseData, textWasLimited);
}
});
},
getLanguageName: function(code)
{
return namespace.Languages[code];
},
getSelectedLanguages: function()
{
var selectedLanguages = {};
var selectedLanguageCodes = this.prefs.getPref('languages.selected').split(',');
$.each(selectedLanguageCodes, function(i, code) {
if(namespace.Languages[code]) {
selectedLanguages[code] = namespace.Languages[code];
}
}.bind(this));
return selectedLanguages;
},
isEnabled: function()
{
return (this.state == namespace.Translator.STATE_ENABLED);
},
isTranslationBySelectionEnabled: function()
{
return this.prefs.getPref('translate.selection');
},
isTranslationByFloatingEnabled: function()
{
return this.prefs.getPref('translate.floating');
},
/* event handlers */
translateSelectionHandler: function(e)
{
// pass event to current page
$(content.document).trigger(e);
},
preferencesChangedHandler: function(e)
{
this.reload();
},
stateSwitchedHandler: function(e)
{
var newState = !(this.state == namespace.Translator.STATE_ENABLED);
this.prefs.setPref('enabled', newState);
},
languageChangedHandler: function(e, languageCode)
{
this.prefs.setPref('language', languageCode);
this.ui.setLanguage(languageCode, this.getLanguageName(languageCode));
// clear cache from any previous translations
this.cache = {};
},
showContextMenuSwitchedHandler: function(e, state)
{
this.prefs.setPref('context.menu', state);
},
showToolbarSwitchedHandler: function(e, state)
{
this.prefs.setPref('toolbar', state);
},
showStatusBarSwitchedHandler: function(e, state)
{
this.prefs.setPref('statusbar', state);
}
};
})(com.igorgladkov.translator, translatorJQuery);